﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="author" content="Jovan Brakus" />
<meta name="description" content="RandomizeR - decision beocme easy" />
<meta name="keywords" content="key, words" /><title>RandomizR</title>
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />
<link rel="shortcut icon" href="img/favicon.ico" />
<script src="js/jquery.js"></script>
<script>

// Randomizer constants
Randomizer.prototype.TickTime = 50;
Randomizer.prototype.TickNumber = 20;
Randomizer.prototype.SlowTicks = 12;
Randomizer.prototype.ResultDelay = 300;

// Randomizer constructor
function Randomizer(question, options) {
    this.question = question;
    this.options = options;
    this.selected = 0;
	this.ticks = this.TickNumber;
}

Randomizer.prototype.SetCaption = function(){
    $("#randtable caption").html(this.question);
};

Randomizer.prototype.GetTickTime = function(){	
    if (this.ticks>0) {
	   return this.TickTime;
	}
	else {
		return this.TickTime + Math.pow(2,Math.abs(this.ticks));
	}
};

Randomizer.prototype.Rand = function(){
    this.ticks = this.ticks - 1;
    this.SetRandom();
    var randObj = this;
    if(this.ticks > (-1)*this.SlowTicks){
        setTimeout(function() { randObj.Rand(); },this.GetTickTime());
    }
    else {
        setTimeout(function() { randObj.EndRand(); },this.ResultDelay);
    }
};

Randomizer.prototype.EndRand = function(){
    $("#randtable tbody").hide();
    this.DisplayResult();
    $("#randtable tbody").fadeIn("slow");
};

Randomizer.prototype.DisplayOptions = function(){
    var optTable = $("#randtable tbody");
    optTable.empty();
    for (var i=0; i<this.options.length; i++) {
      optTable.append('<tr><td class="num">' + (i+1) + '.</td><td>'+this.options[i]+'</td></tr>');
    };
};

Randomizer.prototype.DisplayResult = function(){
    var optTable = $("#randtable tbody");
    optTable.empty();
    optTable.append('<tr class=result><td>' +this.options[this.selected]+'</td></tr>');
    setTimeout('DisplayAgain()', 3000);
};

DisplayAgain = function(){
    var optTable = $("#randtable tbody");
    optTable.empty();
    optTable.append('<tr class=again><td>RandomiZe again...</td></tr>');
    $('table tbody td').bind("click", function() {
        window.location.reload();
        return false;
    });
};

Randomizer.prototype.SetRandom = function(){
    this.selected = parseInt(Math.random()*this.options.length);
    $("#randtable tr").removeClass("selected");
    $("#randtable tr").eq(this.selected).addClass("selected");
};

var randomica = new Randomizer( "Ko pere sudove?", ["Ja", "Ti", "On", "Ona"]);

$(document).ready(function() {
   randomica.SetCaption();
   randomica.DisplayOptions();
   randomica.SetRandom();
   randomica.Rand();
});

</script>
</head>
<body>
<div id="wrap">
<div id="header">
<h2>RandomizR</h2>
</div>
<div id="content">
		<table id="randtable">
			<caption></caption>
			<tbody>					
			</tbody>		
		</table>
</div>
<br/>
<div id="footer">
	<p><img alt="Hosted by Google App Engine" src="./img/appengine.gif"/><a href="http://www.djangoproject.com/"><img src="./img/djangosite.gif" border="0" alt="A Django site." title="A Django site." /></a></p>
    <p>2008, Random.iz.Rs, coding: <a href="http://brakus.iz.rs">Jovan Brakus</a></p>
</div>
</div>
</body>
</html>